package lc20240503;

import java.util.HashMap;
import java.util.Objects;

public class LetterAllotopicWord {
    public static void main(String[] args) {

        String s="hh";
        String t="ah";
        System.out.println("isAnagram(s,t) = " + isAnagram1(s, t));
    }

    public static boolean isAnagram(String s, String t) {
        if (s.length()!=t.length()){
            return false;
        }
        HashMap<Character,Integer> map1=new HashMap<>();
        HashMap<Character,Integer> map2=new HashMap<>();


        for (int i = 0; i < s.length(); i++) {
            char c=s.charAt(i);
            map1.put(c, map1.getOrDefault(c,0)+1);
        }
        for (int i = 0; i < t.length(); i++) {
            char c=t.charAt(i);
            map2.put(c, map2.getOrDefault(c,0)+1);
        }
        for (int i = 0; i < s.length(); i++) {
            if (!Objects.equals(map1.get(s.charAt(i)), map2.get(s.charAt(i)))){
                return false;
            }
        }
        return true;
    }

    public static boolean isAnagram1(String s,String t){
        if (s.length()!=t.length()){
            return false;
        }
        //定义一个数组，因为只有小写字母
        int[] charCount=new int[26];

        for (int i = 0; i < s.length(); i++) {
            //把s.charAt(i)-'a'当作索引
            charCount[s.charAt(i)-'a']++;
        }
        for (int i = 0; i < t.length(); i++) {
            //减去前面加起来的和。
            int i1 = charCount[t.charAt(i) - 'a'];
            i1--;
            if (i1 <0){
                return false;
            }
        }
        return true;
    }
}
